package com.zhiyou100.taxi.acction;

import com.github.pagehelper.PageInfo;

import com.zhiyou100.taxi.entity.Role;
import com.zhiyou100.taxi.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.HashSet;

/**
 * @author zhaokuii11@163.com
 * @create 2021-11-27 10:10
 * @Description
 */
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    RoleService roleService;

    @RequestMapping("/show.action")
    public String show() {
//        return "system/roleList";
        return "forward:/role/page.action";
    }

    @RequestMapping("/page.action")
    public String page(Integer pageNum, HttpServletRequest request) {
        if (pageNum == null) {
            pageNum = 1;
        }
        PageInfo<Role> rolePageInfo = roleService.rolePage(pageNum);
        request.getSession().setAttribute("rolePageInfo", rolePageInfo);
        return "system/roleList";
    }

    @RequestMapping("/save.action")
    public String save(Role role, HttpServletRequest request) {
        role.setRoleId(roleService.getPrimaryKey());
        int num = roleService.saveRole(role);
        if (num > 0) {
            request.setAttribute("smg", "保存成功");
        } else {
            request.setAttribute("msg", "保存失败");
        }
        return "forward:/role/page.action";
    }

    @RequestMapping("/getOneUnion.action")
    public String getOneUnion(String roleId, HttpServletRequest request) {
        Role role = roleService.getOneUnion(roleId);
        request.setAttribute("role", role);

        return "system/role-show";
    }

    @RequestMapping("/update.action")
    public String update(Role role, HttpServletRequest request) {
        int num = roleService.updateByPrimaryKey(role);
        if (num > 0) {
            request.setAttribute("smg", "修改成功");
        } else {
            request.setAttribute("msg", "修改失败");
        }
        return "forward:/role/page.action";
    }

    @RequestMapping("/getOne.action")
    public String getOne(String roleId, HttpServletRequest request) {
        Role role = roleService.getOneByPrimaryKey(roleId);
        request.setAttribute("role", role);
        return "system/role-edit";
    }

    @RequestMapping("/deleteBatch.action")
    public String deleteBatch(String roleIds, HttpServletRequest request) {
        String[] split = roleIds.split(",");
        HashSet<String> set = new HashSet<>(Arrays.asList(split));
        int num = roleService.deleteOneByPrimaryKeys(set);
        if (num > 0) {
            request.setAttribute("smg", "删除成功");
        } else {
            request.setAttribute("msg", "删除失败");
        }
        return "forward:/role/page.action";
    }
}
